local w,h = 370,100
local ui = class.panel:builder 
{
    fdf = 'tooltip_backdrop0',
    x = 426,
    y = 733,
    w = w - 65,
    h = h,
    alpha = 0.99,
    show_flag = false,
}

local w,h = 64,64
ui.buttons = {}

local keys = {'Q','W','E','R','C',}
for a=1,4 do
    local button = ui:add_sp_button((a-1)*(w+1)+20,8,w,h)
    button.has_enter = nil
    button.hotkey = keys[a]
    button.key1 = button:add_panel([[UI\Widgets\Console\Human\human-transport-slot.blp]],2,2,24,24)
    button.key2 = button.key1:add_text(button.hotkey,3,3,0,20,12,4)
    button.key2:set_width(0)
    button:set_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]])
    button.ani_flag = true
    button.title = ui:add_text('金币挑战',button.x-5,button.h,w+10,30,9,5)
    button.index = a
    ui.buttons[a] = button
end


function ui:set_data(data)
    for a,button in ipairs(self.buttons) do
        local skill = data[a]
        button.skill = skill
        if skill then
            button.title:set_text(skill:get_title())
            button:show()
        else
            button:hide()
        end
    end
    self:show()
end

function ui:update()
    for a,button in ipairs(self.buttons) do
        local skill = button.skill
        if skill then
            button.title:set_text(skill:get_title())
        end
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button and button.skill
    if skill then
        local tip = skill:get_tip()
        local tool = ui:skill_tooltip(skill,'top')
        tool.enter_button = button
    end
end

function ui:on_button_clicked(button,key)
    local index = button.index
    if index then
        if key==nil or ac.unit.localunit()==ac.player.self.hero then
            ac.sync('玩家-点击挑战',index)
        end
    end
end

function ui:on_button_right_clicked(button)
    local index = button.index
    if index then
    ac.sync('玩家-自动挑战',index)
    end
end

function ui:on_button_key_down(button)
    if ac.unit.localunit()==ac.player.self.hero then
        button:set_relative_size(0.96,true)
    end
end

function ui:on_button_key_up(button)
    button:set_relative_size(1,true)
end

function ui:refresh(button)
    if button and button.key1 then
        if ac.unit.localunit()==ac.player.self.hero then
            button.key1:show()
        else
            button.key1:hide()
        end
        
    end
end


ac.game:event '玩家-设置挑战内容'(function(_,player,data)
    if player:is_self() then
        ui:set_data(data)
    end
end)

ac.hardware:event '界面绘制'(function(_,frame)
    ui:notify('update',frame)
end)
